<?php
namespace app\app\controller;
use app\model\EquipmentModel;
use app\model\UserModel;
use think\App;
use think\Controller;
use app\model\AppUserModel;
use app\model\MenuModel;

class Login extends Controller
{
    //轮播图页面
    public function lunbo(){
        return view('index');
    }
    //高德实验接口
    public function gaode()
    {

//        header("content-type:text/html;charset=utf-8");
//        header('Content-Type:text/html;charset=utf-8');
//        $url='https://www.amap.com/service/poiInfo?query_type=TQUERY&keywords=河北省北斗导航位置服务有限公司';
        $u='河北省北斗导航位置服务有限公司';
//        $u='唐荣天润新城';
//        $u='alksszdcsa';
        //latitude  黄纬
        //longitude  黄经
        $u= urlencode($u);//转url编码
        $url="https://www.amap.com/service/poiInfo?query_type=TQUERY&keywords={$u}";
        $data=file_get_contents($url);
        $data=json_decode($data,true);
        if ($data['data']['code']==1){//有找到相应地址
            $poi=$data['data']['poi_list'];//匹配地址数组
            $datas=[];
          $bd_encr=  bd_encrypt($poi[0]['longitude'],$poi[0]['latitude']);
//            dump($data['data']['poi_list']);
            dump($bd_encr);
//            foreach ($poi as )
            dump($poi[0]['longitude']);
            dump($poi[0]['latitude']);die();

        }else{
            dump('未找到');
            die();
        }
        $longitude=$data[''];
        $latitude='';
        return view();
    }
    //注册页面
    public function reg(){
        return view('reg');
    }
    //注册
    public function regData(){
        $getData= input('post.');
        $where=['phone'=>$getData['mobile'],
        ];
        $getUser=AppUserModel::where($where)->find();//手机是否存在
        if ($getUser){return retJson('','201','该手机已经注册');}
//        $getEquipment=EquipmentModel::where('name',$getData['imei'])//检查设备号
//            ->where('equ_user',0)//未绑定
//            ->find();
//
//        setDatabaseDef($getEquipment,'设备号有误');
        //验证码
        $code=session($getData['mobile'])['code'];
        if ((string)$code!=$getData['code']){
            return retJson('','201','验证码错误');
        }
        $insert = [
            'phone'=>$getData['mobile'],
            'password'=>encryptPass($getData['password2']),
            'nickname'=>$getData['mobile'],//昵称（先使用手机号）
//            'imei'=>$getData['imei'],//设备号
            'add_time'=>time(),
        ];
        $addUser=AppUserModel::insertGetId($insert);//添加用户

        $data['name'] = $getData['mobile'];
        $data['user_id'] = $addUser;
        $info = MenuModel::insertGetId($data);

//        $updata=['equ_user'=>$addUser,
////                 'nickname'=>substr($getEquipment['name'],-4),//设备名--昵称（设备号后4位）
//                 'expire_time'=>time(),
//            ];
//        EquipmentModel::where('id',$getEquipment->id)->update(['equ_user'=>$addUser,'expire_time'=>time()]);
        return setDatabase($addUser,'用户注册');
    }
    /**
     * @return \think\response\View
     * APP登陆页面
     */
    public function index(){
        return view('login');
    }

    /**
     * @return \think\response\Json
     * 登陆功能
     */
    public function loginData()
    {
        $getData=input('post.');
        $where=['phone'=>$getData['phone'],'password'=>encryptPass($getData['password'])];
        $getUser=AppUserModel::where($where)->find();
        if (!$getUser){
         return retJson('',201,'用户名或密码错误');
        }
        $geydata=EquipmentModel::where('equ_user',$getUser['id'])->column('name');
        session('dev_name',$geydata); //用户绑定的设备信息
        session('appuser',$getUser);//用户个人信息
        return retJson('',200,'登陆成功');

    }
    /*
      * 退出登陆
      */
    public function outLogin()
    {
        session_start();
        session_destroy();
        $this->redirect('Login/index');
    }
    /*
     * 忘记密码界面
     */
    public function for_pw()
    {
        return view('for_pw');
    }
    /*
     * 更新密码
     */
    public function forData()
    {
        $getData=input('post.');
        $where=['phone'=>$getData['mobile'],
        ];
        $getUser=AppUserModel::where($where)->find();//手机是否存在
        if (!$getUser){return retJson('','201','该手机未注册');}
        //验证码
        $code=session($getData['mobile'])['code'];
        if ((string)$code!=$getData['code']){
            return retJson('','201','验证码错误');
        }
        $updata = [
            'id'=>$getUser->id,
            'password'=>encryptPass($getData['password2']),
        ];
        $setUser=AppUserModel::update($updata);
        return setDatabase($setUser,'密码重置');
    }
    /*
     *短信 ----注册用户
     */
    public function vercod()
    {
//        cookie('time',null);die();
        $getData=input('post.');

        if (cookie('time') == 1){
            return retJson('', 201, '距离上次验证码发送不超过一分钟！');
        }
        $code = rand(1000,9999);
        $mobile = input('mobile');

        $res =AppUserModel::field('*')->where('phone',$mobile)->find();
        if (isset($getData['for_pw'])){//忘记密码
            if (!$res){
                return retJson('', 201, '手机号未注册');
            }
        }else{
            if ($res){
                return retJson('', 201, '手机号已注册！');
            }
        }

        session($mobile,array('mobile'=>$mobile,'code'=>$code));
        $send = sendMsg($mobile,$code);
        if ($send['Message'] != 'OK'){
            return retJson('', 201, '短信发送失败！');
        }
        cookie('time',1,60);
//        return retJson('', 200, session($mobile));
        return retJson('', 200, '短信已发送！');
    }

    //协议页面
    public function agreement(){
        return view('agreement');
    }
}